/**
 * @param ['post'|'get'] type 请求方式
 * @param string url 接收请求的地址
 * @param object jsonParam请求参数
 * @param Callback succ_cb_func接收响应的回调函数
 */
function ajax(obj,isShowLoad) {
    if(isShowLoad){
        $('.modal_Load').show();
    }
    
    obj.data.token_ = window.localStorage.JinDouCheToken;
    $.ajax({
        'type':obj.type,
        'url':obj.url,
        'dataType':obj.dataType,
        'data':obj.data,
        'success':function (response) {
          if(isShowLoad){
              $('.modal_Load').hide();
          }
          if(response && response.error == "40002"){
              window.location.href="/admin/login";
              return false;
          }else if(response && response.error == "40004"){
              swal({ 
                title: "您的账号已在其他设备登录！", 
                text: "10秒后跳转到登录。", 
                timer: 1000*10, 
                showConfirmButton: false 
              },function(){
                top.location.href = '/admin/login';
              });
              return false;
          }else{
            obj.success(response);
          }
          
        },
        'error':function (error) {
            if(isShowLoad){
                $('.modal_Load').hide();
            }
            sweetAlert("", error.responseJSON.message, "error");
        },
        'xhrFields':obj.xhrFields
    });
}

/**
 * @param string url 接收请求的地址
 * @param object data 请求参数
 * @param Callback success 接收响应的回调函数
 * @param ['json'|'xml'] dataType 接收响应的回调函数
 */
function post(url, data, success, dataType,isShowLoad) {
    if(isShowLoad){
        $('.modal_Load').show();
    }
    data.token_ = window.localStorage.JinDouCheToken;
    $.ajax({
        'type':'post',
        'url':url,
        'dataType':dataType,
        'data':data,
        'success':function (response) {
            if(isShowLoad){
                $('.modal_Load').hide();
            }
          if(response && response.error == "40002"){
              window.location.href="/admin/login";
              return false;
          }else if(response && response.error == "40004"){
              swal({ 
                title: "您的账号已在其他设备登录！", 
                text: "10秒后跳转到登录。", 
                timer: 1000*10, 
                showConfirmButton: false 
              },function(){
                top.location.href = '/admin/login';
              });
              return false;
          }else{
            success(response);
          }
          
        },
        'error':function (error) {
            if(isShowLoad){
                $('.modal_Load').hide();
            }
            sweetAlert("", error.responseJSON.message, "error");
        }
    });
}

/**
 * @param string url 接收请求的地址
 * @param object data 请求参数
 * @param Callback success 接收响应的回调函数
 * @param ['json'|'xml'] dataType 接收响应的回调函数
 */
function get(url, data, success, dataType,isShowLoad) {
    if(isShowLoad){
        $('.modal_Load').show();
    }
    data.token_ = window.localStorage.JinDouCheToken;
    $.ajax({
        'type':'get',
        'url':url,
        'dataType':dataType,
        'data':data,
        'success':function (response) {
          if(isShowLoad){
            $('.modal_Load').hide();
          }
          if(response && response.error == "40002"){
              window.location.href="/admin/login";
              return false;
          }else if(response && response.error == "40004"){
               swal({ 
                title: "您的账号已在其他设备登录！", 
                text: "10秒后跳转到登录。", 
                timer: 1000*10, 
                showConfirmButton: false 
              },function(){
                top.location.href = '/admin/login';
              });
              return false;
          }else{
            success(response);
          }
          
        },
        'error':function (error) {
            if(isShowLoad){
                $('.modal_Load').hide();
            }
            sweetAlert("", error.responseJSON.message, "error");
        }
    });
}

/**
 * url参数添加
 * @param str url 完整的url
 * @param str param_k 需要添加的参数
 * @param obj|bool this_ 包含val属性的元素或false
 * @param str valn 参数值 ---- 在this_为false的情况下次参数是必填的
 */
function paramAddUrl(url,param_k,this_,valn) {
  if(this_){
    var html_b = $(this_).attr('val');
  }else{
    var html_b = valn;
  }
  var arr_url = url.split('?');
  if(arr_url.length==2){
    arr_url[1] = arr_url[1].split('#')[0];
    var brand_p = arr_url[1].split('&');
    var url_r = new Array();
    var ii = 0;
    var istrue = false;
    var param = param_k+'='+html_b;
    for (var i = 0; i < brand_p.length; i++) {
      var brand_l = brand_p[i].split('=');
      if(brand_l[0]==param_k){
        url_r[ii] = param;
        istrue = true;
      }else if(brand_l[0]!=param_k&&brand_l[0]!='page'&&brand_l[0]!='searchkey'&&brand_l[0]!='p'){
        url_r[ii] = brand_p[i];
      }
      ii++;
    }
  }else{
    var param = param_k+'='+html_b;
    arr_url[0] = url.split('#')[0];
    var param_url = param;
  }
  if(!istrue&&!param_url){
    var param_url = url_r.join('&')+'&'+param;
  }else if(istrue&&!param_url){
    var param_url = url_r.join('&');
  }
  sessionStorage[param_k] = html_b;
  return arr_url[0]+'?'+param_url;
}

function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

function ExcelExport(typeid){
        var exportType = typeid;
        post("/admin/order/ExcleFinance",{exportType:exportType},function(res){
                if(res.success=='OK'){
                    location.href = '/Admin/Channel/exportExcel?filename='+res.filename;
                    return true;
                }else{
                    alert("文件生成失败");return false;
                }
        },'json');
    }


function pageData(page)
{
    var end = page * 15;
    var start = end - 15;
    var result = [];
    var id = getItem('orders');
    var data = JSON.parse(Base64.decode(id.substring(4, id.length)));
    for (var i = start; i < end; i ++ ) {
        if (data && data[i]) {
            result.push(data[i]);
        }
    }
    return result;
}

function showPage(page, totalPage, addInfo)
{
    if (totalPage <= 1) {
        $('.black2').children().remove();
        return;
    }
    var start = 0;
    var end = 0;
    if (page >= 7 && page <= (totalPage - 5) && totalPage >= 11) {
        start = page - 5;
        end = page + 5;
    } else if (page >= (totalPage - 5) && totalPage >= 11) {
        start = totalPage - 10;
        end = totalPage;
    } else if (page <= 6 && totalPage >= 11) {
        start = 1;
        end = 11;
    } else {
        start = 1;
        end = totalPage;
    }

    var str = '';
    if (page >= 7) {
        str += '<a class="first" href="javascript:void(0)" onClick="addInfo(1,'+totalPage+')">首页</a>'
    }
    if (page > 1) {
        var prev = page - 1;
        str += '<a class="prev" href="javascript:void(0)" onClick="addInfo('+prev+','+totalPage+')">上一页</a>';
    }
    for (var i = start; i <= end; i++) {
        if (i == page) {
            str += "<span class='current'>"+i+"</span>";
            continue;
        }
        str += '<a class="num" href="javascript:void(0)" onClick="addInfo('+i+','+totalPage+')">'+i+'</a>';
    }

    if (page != totalPage) {
       var next =  page + 1;
        str += '<a class="next" href="javascript:void(0)" onClick="addInfo('+next+','+totalPage+')">下一页</a>';
    }
    if (page != totalPage && totalPage >= 7) {
        str += '<a class="end" href="javascript:void(0)" onClick="addInfo('+totalPage+','+totalPage+')">尾页</a>';
    }
    $('.black2').children().remove();
    $('.black2').append(str);
}

function setItem(tag, data)
{
    var id = Base64.encode(JSON.stringify(data));
    id = 'AAAA'+id;
    window.localStorage.setItem(tag, id);
}

function getItem(tag)
{
    return window.localStorage.getItem(tag);
}

function base64_encode(data)
{
    var id = Base64.encode('bkfx'+JSON.stringify(data));
    return id;
}

function base64_decode(id)
{
    return JSON.parse(Base64.decode(id.substring(4, id.length)));
}

var filterIndexs = [];
var filterNames = [];
var filterTag = 1;

function selectChecked()
{
    var checkBox = $('.tr_checkbox th');
    filterIndexs = [];
    for (var i = 0; i < checkBox.length; i++) {
        $('.tr_checkbox th').eq(i).children().eq(0).prop('checked', true);
        filterIndexs.push(i);
    }
}

function tableHide()
{
    var checkBox = $('.tr_checkbox th');
    for (var i = 0; i < checkBox.length; i++) {
        if (!in_array(i, filterIndexs)) {
            $('.tr_checkbox th').eq(i).hide();
        } else if (!filterTag) {
            $('.tr_checkbox th').eq(i).children().eq(0).prop('checked', true);
        }
    }
    var childrens = $('.table_hover tr');
    for (var i = 0; i < childrens.length; i++) {
        var tds = $('.table_hover tr').eq(i).children();
        for (var j = 0; j < tds.length; j++) {
            if (!in_array(j, filterIndexs)) {
                $('.table_hover tr').eq(i).children().eq(j).hide();
            }
        }
    }
    var tilteChildrends = $('.tr_item_title th');
    for (var i = 0; i < tilteChildrends.length; i++) {
        if (!in_array(i, filterIndexs)) {
            $('.tr_item_title th').eq(i).hide();
        }
    }
    $('#btn_filter').show();
    $('#btn_recovery').show();
    if ($('td').hasClass('action_list')) {
        $('.action_list').show();
    }
}

function tableShow()
{   
    filterIndexs = [];
    var checkBox = $('.tr_checkbox th');
    for (var i = 0; i < checkBox.length; i++) {
        filterIndexs.push(i);
        $('.tr_checkbox th').eq(i).show();
        $('.tr_checkbox th').eq(i).children().eq(0).prop('checked', true);
    }

    var childrens = $('.table_hover tr');
    for (var i = 0; i < childrens.length; i++) {
        var tds = $('.table_hover tr').eq(i).children();
        for (var j = 0; j < tds.length; j++) {
            $('.table_hover tr').eq(i).children().eq(j).show();
        }
    }

    var tilteChildrends = $('.tr_item_title th');
    for (var i = 0; i < tilteChildrends.length; i++) {
        $('.tr_item_title th').eq(i).show();
    }

    $('#btn_filter').show();
    $('#btn_recovery').show();
}

function in_array(number, filters)
{
    for (var i = 0; i < filters.length; i++) {
        if (number == filters[i]) {
            return true;
        }
    }
    return false;
}

$('body').on('click', '.checkbox_input',function(){
    var value = $(this).parent().index();
    if (in_array(value, filterIndexs)) {
        var index = filterIndexs.indexOf(value);
        filterIndexs.splice(index, 1);
        return;
    }
    filterIndexs.push(value);
});

$('body').on( 'click', '.btn_filter', function(){
    var checkBox = $('.tr_checkbox th');
    if (checkBox.length == filterIndexs.length) return;
    // if (!filterTag) return;
    tableHide();
    filterTag = 0;
});

$('body').on('click', '.btn_recovery', function(){
    tableShow();
    filterTag = 1;
});

function recovery()
{
    // tableShow();
    selectChecked();
    filterTag = 1;
}

function addNotify()
{
    var str = '';
    var children = $('.tr_item_title th');
    if (children.length >= 20) {
        var count = Math.ceil(children.length / 3);
        str += "<td colspan='"+count+"'>暂无数据</td>";
        str += "<td colspan='"+count+"'>暂无数据</td>";
        str += "<td colspan='"+count+"'>暂无数据</td>";
    } else {
        var count = children.length;
        str += "<td colspan='"+count+"'>暂无数据</td>";
    }
    var str2 = "<tr>"+str+"</tr>";
    $('.table_hover tr').remove();
    $('.black2').children().remove();
    $('.table_hover').append(str2);
}
function isIntNum(val){
    if (val == 0 || val == '0') {
        return true;
    } else {
        if (parseInt(val)) {
            return true;
        } else {
            return false;
        }
    }
}